<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket客户端</title>
</head>
<body>

<script type="text/javascript">
    var socket;
    var tt;
    var lockReconnect = false;//避免重复连接
    var wsUrl = "ws://localhost:8899/ws";
    createWebSocket();
    function createWebSocket() {
        if (window.WebSocket) {
            socket = new WebSocket(wsUrl);
            init();
        }else {
            alert("浏览器不支持WebSocket!");
        }
    }

    function init() {
        /** 当客户端接收到服务器发送给的消息时,onmessage方法会被调用 **/
        socket.onmessage = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value+"\n"+ev.data;
            heartCheck.start();
        }
        /** 当连接一旦打开,onopen方法会被调用 **/
        socket.onopen = function () {
            var ta = document.getElementById("responseText");
            ta.value = "连接开启! "
            heartCheck.start();
        }
        /** 当连接一旦断开,onclose方法会被调用 **/
        socket.onclose = function () {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + "连接关闭! "
            reconnect(wsUrl);
        }
        socket.onerror = function () {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + "发生异常! "
            reconnect(wsUrl);
        }
    }

    function reconnect() {
        if (lockReconnect) return;
        lockReconnect = true;
        // 没连上会一直重连,设置延迟避免请求过多
        tt && clearTimeout(tt);
        tt = setTimeout(function () {
            createWebSocket();
            lockReconnect = false;
        },4000)
    }

    var heartCheck = {
        timeout : 3000,
        timeoutObj : null,
        serverTimeoutObj : null,
        start : function () {
            console.log('start');
            var self = this;
            this.timeoutObj && clearTimeout(this.timeoutObj);
            this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
            this.timeoutObj = setTimeout(function () {
                // 这里发送一个心跳,后端接收后,返回一个心跳消息,
                console.log('timeoutObj ... ');
                socket.send("heartCheck");
                console.log('heartCheck ... ');
                self.serverTimeoutObj = setTimeout(function () {
                    console.log('serverTimeoutObj ... ');
                    socket.close();
                },self.timeout);
            },this.timeout);

        }
        
    };


    function send(message) {
        if (socket.readyState = WebSocket.OPEN){
            socket.send(message);
        } else {
            alert("连接未开启! ")
        }
    }


</script>

<form action="" onsubmit="false">

    <textarea name="message" style="width: 400px; height: 200px"></textarea>
    <input type="button" value="发送数据" onclick="send(this.form.message.value)">
    <h3>服务端输出: </h3>
    <textarea id="responseText" style="width: 400px; height: 200px"></textarea>

    <input type="button" onclick="javascript: document.getElementById('responseText').value=''" value="清空内容">


</form>
</body>
</html>